<template>
  <div class="navigation">
    <!-- Logo 图片 -->
    <img :src="logoUrl" alt="Logo" class="logo" />
    <router-link to="/">首页</router-link>
    <router-link to="/pet-products">宠物用品</router-link>
    <router-link to="/pet-adoption">宠物领养</router-link>
    <router-link to="/pet-lost">宠物挂失</router-link>
    <router-link to="/forum">论坛信息</router-link>
    <router-link to="/profile">个人中心</router-link>
    <router-link to="/Pet-AdoptionRegistration">领养进度</router-link>
    <router-link 
      to="/shopping-cart" 
      class="cart-link" 
      @mouseover="hoverPreviewVisible = true" 
      @mouseleave="hoverPreviewVisible = false"
    >
      购物车
      <span class="cart-badge" :class="{ 'animate': animateBadge, 'hidden': cartCount === 0 }">
        {{ cartCount }}
      </span>
    </router-link>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';

const logoUrl = new URL('/src/assets/images/logo.jpg', import.meta.url).href;
const cartCount = ref(2); // 初始购物车数量
const lastAddedItem = ref(null); // 最近添加的商品
const animateBadge = ref(false);
const hoverPreviewVisible = ref(false);
const router = useRouter();

// 添加商品到购物车
const addToCart = (item) => {
  lastAddedItem.value = item;
  cartCount.value += item.quantity;
  triggerBadgeAnimation();
};

// 触发徽章动画
const triggerBadgeAnimation = () => {
  animateBadge.value = true;
  setTimeout(() => {
    animateBadge.value = false;
  }, 300);
};

// 移除商品（示例方法）
const removeFromCart = (item) => {
  cartCount.value -= item.quantity;
  triggerBadgeAnimation();
};
</script>

<style scoped>
.navigation {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 15px 20px;
  background-color: #4a7c59; /* 背景颜色 */
  color: #ffffff; /* 字体颜色 */
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* 阴影效果 */
}

.logo {
  width: 80px; /* Logo 宽度 */
  height: auto;
}

.navigation a {
  color: #ffffff; /* 链接颜色 */
  text-decoration: none;
  margin: 0 15px; /* 链接间距 */
  font-weight: 500; /* 字体粗细 */
  transition: color 0.3s; /* 悬停效果过渡 */
}

.navigation a:hover {
  color: #d4a373; /* 链接悬停颜色 */
}

.cart-link {
  position: relative;
  display: flex;
  align-items: center;
  font-weight: bold;
}

.cart-badge {
  background-color: #ff4444; /* 徽章背景颜色 */
  color: #ffffff; /* 徽章字体颜色 */
  border-radius: 50%;
  padding: 4px 8px;
  font-size: 0.8rem;
  margin-left: 8px;
  transition: transform 0.3s ease;
}

.cart-badge.animate {
  transform: scale(1.2);
}

.cart-badge.hidden {
  display: none;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .navigation {
    flex-direction: column;
    align-items: flex-start;
    padding: 10px;
  }

  .navigation a {
    margin: 5px 0; /* 调整链接间距 */
  }

  .logo {
    width: 60px; /* 调整 Logo 宽度 */
  }
}
</style>

<style scoped>
.navigation {
  width: 98%;
  display: flex;
  font-size: 1.4rem;
  justify-content: center;
  margin: 10px;
  background-color: #f8f4e9; /* 清新、温暖的背景色 */
  border-radius: 20px; /* 圆角设计 */
  align-items: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1); /* 阴影效果 */
}

.logo {
  width: 50px; 
  height: 50px; 
  border-radius: 50%; /* 圆形 logo */
  margin-right: 10px; 
  margin-left: 10px;
  object-fit: cover; 
}

.navigation a {
  margin: 0 10px;
  text-decoration: none;
  color: #4a7c59; /* 主色 */
  font-weight: bold;
  transition: color 0.3s ease; /* 平滑的颜色过渡 */
}

.navigation a:hover {
  text-decoration: underline;
  color: #ff8f00; /* 悬停时的颜色变化 */
}

.cart-link {
  position: relative;
  display: flex;
  align-items: center;
}

.cart-badge {
  background-color: #ff4444; /* 徽章背景色 */
  color: white;
  border-radius: 50%;
  padding: 4px 8px;
  font-size: 0.8rem;
  transition: transform 0.3s ease;
}

.cart-badge.animate {
  transform: scale(1.2);
}

.cart-badge.hidden {
  display: none;
}

.hover-preview {
  position: absolute;
  top: 120%;
  left: 50%;
  transform: translateX(-50%);
  background: white;
  padding: 15px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  z-index: 1000;
  min-width: 200px;
}

.hover-preview h3 {
  margin-top: 0;
  color: #ff4444;
}

.cart-link:hover .hover-preview {
  display: block;
}
</style>